package com.doangeometrydash.manager;

import java.io.IOException;
import org.andengine.audio.music.Music;
import org.andengine.audio.music.MusicFactory;
import org.andengine.audio.sound.Sound;
import org.andengine.audio.sound.SoundFactory;
import org.andengine.engine.Engine;
import org.andengine.engine.camera.BoundCamera;
import org.andengine.extension.tmx.TMXLoader;
import org.andengine.extension.tmx.TMXTiledMap;
import org.andengine.extension.tmx.util.exception.TMXLoadException;

import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import com.doangeometrydash.extra.StoredData;
import com.doangeometrydash.main.GameActivity;

public class ResourcesManager
{
	private static final ResourcesManager INSTANCE = new ResourcesManager();
	public Engine mEngine;
	public GameActivity gameActivity;
	public BoundCamera mCamera;
	public VertexBufferObjectManager vbom;

	// Splash Texture
	public ITextureRegion splash_region;
	private BitmapTextureAtlas splashTextureAtlas;

	// Menu Texture
	public ITextureRegion menu_background_region;
	public ITextureRegion play_region;
	public ITextureRegion options_region;
	public ITextureRegion information_region;
	private BitmapTextureAtlas menuTextureAtlas;

	// Game Font
	public Font gameFont;

	// Menu Font
	public Font font;

	// Player Texture
	public ITextureRegion player_region;
	private BitmapTextureAtlas playerTextureAtlas;

	// GameOver texture
	public ITextureRegion game_over_region;
	public ITextureRegion play_again_region;
	private BitmapTextureAtlas gameOverTextureAtlas;

	// Options texture
	public ITextureRegion option_background_region;
	public ITextureRegion check;
	public ITextureRegion uncheck;
	private BitmapTextureAtlas optionTextureAtlas;

	// About texture
	public ITextureRegion about_region;
	private BitmapTextureAtlas aboutTextureAtlas;

	// Music
	public Music soundTrackGame;
	public Music soundTrackMenu;
	public Sound explosion;

	// options
	private StoredData storeOpt;
	public boolean retryOption;
	public boolean muteOption;

	public TMXTiledMap tmxMap;

	public void loadSplashScreen()
	{
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		splashTextureAtlas = new BitmapTextureAtlas(gameActivity.getTextureManager(), 500, 500, TextureOptions.BILINEAR);
		splash_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(splashTextureAtlas, gameActivity, "splash.png", 0, 0);
		splashTextureAtlas.load();
	}

	public void unloadSplashScreen()
	{
		splashTextureAtlas.unload();
		splash_region = null;
	}

	public void loadOptionResources()
	{
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/menu/");
		optionTextureAtlas = new BitmapTextureAtlas(gameActivity.getTextureManager(), 1000, 1000, TextureOptions.DEFAULT);

		option_background_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(optionTextureAtlas, gameActivity, "optionbackground.png",
				0, 0);
		check = BitmapTextureAtlasTextureRegionFactory.createFromAsset(optionTextureAtlas, gameActivity, "check.png", 0, 500);
		uncheck = BitmapTextureAtlasTextureRegionFactory.createFromAsset(optionTextureAtlas, gameActivity, "uncheck.png", 540, 0);
		optionTextureAtlas.load();
		loadOptionMenuFont();
	}

	private void loadOptionMenuFont()
	{
		FontFactory.setAssetBasePath("font/");

		gameFont = FontFactory.createFromAsset(gameActivity.getFontManager(), gameActivity.getTextureManager(), 1000, 60, gameActivity.getAssets(),
				"ALGER.TTF", 30, true, android.graphics.Color.WHITE);
		gameFont.load();
	}

	public void loadAboutResources()
	{
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/menu/");
		aboutTextureAtlas = new BitmapTextureAtlas(gameActivity.getTextureManager(), 480, 320, TextureOptions.DEFAULT);
		about_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(aboutTextureAtlas, gameActivity, "about.png", 0, 0);
		aboutTextureAtlas.load();
	}

	public void loadGameOverResources()
	{
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/gameover/");
		gameOverTextureAtlas = new BitmapTextureAtlas(gameActivity.getTextureManager(), 560, 320, TextureOptions.DEFAULT);
		game_over_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(gameOverTextureAtlas, gameActivity, "gameover.png", 0, 0);
		play_again_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(gameOverTextureAtlas, gameActivity, "playagain.png", 481, 0);
		gameOverTextureAtlas.load();
	}

	public void loadMenuResources()
	{
		loadMenuGraphics();
		loadMenuAudio();
		loadMenuFont();
	}

	private void loadMenuFont()
	{
		FontFactory.setAssetBasePath("font/");

		font = FontFactory.createFromAsset(gameActivity.getFontManager(), gameActivity.getTextureManager(), 256, 256, gameActivity.getAssets(),
				"font.TTF", 50, true, android.graphics.Color.RED);
		font.load();
	}

	private void loadMenuGraphics()
	{
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/menu/");
		menuTextureAtlas = new BitmapTextureAtlas(gameActivity.getTextureManager(), 1000, 1000, TextureOptions.BILINEAR);
		menu_background_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, gameActivity, "background.png", 0, 0);
		play_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, gameActivity, "play.png", 500, 0);
		options_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, gameActivity, "options.png", 0, 500);
		information_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, gameActivity, "information.png", 0, 700);
		menuTextureAtlas.load();
	}

	private void loadMenuAudio()
	{
		storeOpt = new StoredData();
		retryOption = storeOpt.read("isCheckAR", gameActivity);
		muteOption = storeOpt.read("isCheckMusic", gameActivity);

		MusicFactory.setAssetBasePath("sound/");
		try
		{
			soundTrackMenu = MusicFactory.createMusicFromAsset(this.mEngine.getMusicManager(), gameActivity.getApplicationContext(), "menu.ogg");
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		soundTrackMenu.setLooping(true);
	}

	public void loadGameResources(int mapLevel)
	{
		loadGameAudio(mapLevel);
		loadGameGraphics();
		loadTMXMap("tmx/map" + mapLevel + ".tmx");
		loadGameFonts();
	}

	private void loadGameGraphics()
	{
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/player/");
		playerTextureAtlas = new BitmapTextureAtlas(gameActivity.getTextureManager(), 25, 25);
		player_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(playerTextureAtlas, gameActivity.getAssets(), "box.png", 0, 0);
		playerTextureAtlas.load();
	}

	private void loadTMXMap(String mapName)
	{
		TMXLoader loader = new TMXLoader(gameActivity.getAssets(), gameActivity.getTextureManager(), vbom);
		try
		{
			tmxMap = loader.loadFromAsset(mapName);
			tmxMap.setOffsetCenter(0, 0);
		}
		catch (TMXLoadException e)
		{
			e.printStackTrace();
		}
	}

	private void loadGameFonts()
	{
		FontFactory.setAssetBasePath("font/");

		gameFont = FontFactory.createFromAsset(gameActivity.getFontManager(), gameActivity.getTextureManager(), 1000, 60, gameActivity.getAssets(),
				"ALGER.TTF", 60, true, android.graphics.Color.WHITE);
		gameFont.load();
	}

	private void loadGameAudio(int mapLevel)
	{
		MusicFactory.setAssetBasePath("sound/");
		try
		{
			switch (mapLevel)
			{
			case 1:
				soundTrackGame = MusicFactory.createMusicFromAsset(this.mEngine.getMusicManager(), gameActivity.getApplicationContext(), "music.ogg");
				break;
			case 2:
				soundTrackGame = MusicFactory.createMusicFromAsset(this.mEngine.getMusicManager(), gameActivity.getApplicationContext(), "xStep.ogg");
				break;
			}
		}
		catch (IllegalStateException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		soundTrackGame.setLooping(true);

		SoundFactory.setAssetBasePath("sound/");
		try
		{
			explosion = SoundFactory.createSoundFromAsset(gameActivity.getSoundManager(), gameActivity, "explosion.ogg");
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	public static void prepareManager(Engine engine, GameActivity activity, BoundCamera camera, VertexBufferObjectManager vbom)
	{
		getInstance().mEngine = engine;
		getInstance().gameActivity = activity;
		getInstance().mCamera = camera;
		getInstance().vbom = vbom;
	}

	public static ResourcesManager getInstance()
	{
		return INSTANCE;
	}

	public void storeData()
	{
		storeOpt.write("isCheckMusic", getInstance().muteOption, getInstance().gameActivity);
		storeOpt.write("isCheckAR", getInstance().retryOption, getInstance().gameActivity);
	}
}
